package com.csdj.t309springboot.kaoshi.controller;
import com.csdj.t309springboot.kaoshi.entity.User;
import com.csdj.t309springboot.kaoshi.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    private Logger log = LoggerFactory.getLogger("test");




    @GetMapping("login")
    public String toLogin(){
        log.info("toLogin");
        //转到login.html
        return "login";
    }
    @GetMapping("logout")
    public String logout(){
        //转到login.html
        return "login";
    }

    @GetMapping("index")
    public String index() {
        return "index";
    }
    @GetMapping("chaxun")
    public String chaxun() {
        return "chaxun";
    }

    @PostMapping("doLogin")
    public String doLogin(/*@RequestParam */String account, String password, Model model,
                          HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        log.info("doLogin()==, account="+account+",password="+password);
        PrintWriter out = response.getWriter();
        User user = userService.login(account, password);
        if(null != user){ //

            model.addAttribute("name",user.getUserName());


            //重定向到首页 redirect:
            return "redirect:/chaxun";
        }
        //用户名或密码错误
        out.println("<script>alert('用户名或密码错误')</script>");
//        model.addAttribute("error","用户名或密码错误");
        return "login";
    }




}
